<!-- 个人动态页 -->
<!-- 小旗子 2020/8/22日更新-->

<!-- 小桃子 更新于2020.09.02 -->
<!-- 更新：1.分页获取数据
					2.布局 -->

<!-- 小桃子 更新于2020.09.10 -->
<!-- 修改：微调样式 -->
<template>
	<view class="live">
		<!-- 自定义导航栏(navigationbar) -->
		<view class="nav">
			<view :class="activeSwiper == 0 ? 'listTitle_active' : 'listTitle'" @tap="swichTab" :data-index="0"><text>帖子</text></view>
			<view :class="activeSwiper == 1 ? 'listTitle_active' : 'listTitle'" @tap="swichTab" :data-index="1"><text>文章</text></view>
		</view>
		<view class="top_box"></view>
		<swiper 
			class="swiper_box" 
			:style="'height:' + swiperItemHeight + 'px;' + 'transition: height 0.5s;'" 
			@change="swiperChange" 
			:current="activeSwiper">
			<swiper-item>
				<view class="content" :class="activeSwiper == 0 ? 'page page_active' : 'page'">
					<PostingsSummarys v-for="(item,index) in personDynamic" :key="index" :datas="item" :isNav="true"></PostingsSummarys>
					<view style="width: 100%;height: 5px;"></view>
					<UniLoadMore :status="dynamicStatus" :contentText="contentText" color="#7f7f7f"></UniLoadMore>
					<view class="top_box"></view>
				</view>
			</swiper-item>
			<swiper-item>
				<view :class="activeSwiper == 1 ? 'page page_active' : 'page'">
					<AcademicSummary v-for="(item,index) in personAcademic" :key="index" :datas="item"></AcademicSummary>
					<view style="width: 100%;height: 5px;"></view>
					<UniLoadMore :status="academicStatus" :contentText="contentText" color="#7f7f7f"></UniLoadMore>
					<view class="top_box"></view>
				</view>
			</swiper-item>
		</swiper>	
	</view>
</template>

<script>
	import AcademicSummary from "../../components/summary/AcademicSummary.vue"
	import PostingsSummarys from "../../components/summary/PostingsSummarys.vue"
	import UniLoadMore from '../../components/plugins/uni-load-more.vue'
	import {_ajax} from '../../in-xupt-functions.js'
	export default {
		onLoad(options) {
			let that=this;
			uni.showLoading({
				mask: true,
				title: '正在加载'
			})
			let _self = this
			// uni.getSystemInfo({
			// 	success: function(res) {
			// 		_self.screenWidth = res.windowWidth
			// 		_self.screenHeight = res.windowHeight
			// 	}
			// })
			uni.getStorage({
				key: 'userId',
				success(res) {
					_self.userId = res.data
					_ajax({
						url: '/user/'+_self.userId+'/page',
						method: 'get'
					}).then(res => {
						_self.personMessage = res.data.data
						return _ajax({ // 获取动态
							url: '/user/'+_self.userId+'/posts'+'?pageNum='+_self.dynamicNowpage+'&pageSize='+_self.pageSize,
							method: 'get'
						})
					}).then(res => {
							if(res.data.status == 0) {
								_self.personDynamic.push(...res.data.data.list)
								_self.dynamicIsLastpage = res.data.data.isLastPage
								_self.dynamicStatus = 'more'
								if(_self.personDynamic.length == 0) {
									_self.dynamicStatus = 'noMore'
								}
							}
							that.getheight(200)
							return _ajax({ // 获取文章
								url: '/user/'+_self.userId+'/articles'+'?pageNum='+_self.academicNowPage+'&pageSize='+_self.pageSize,
								method: 'get'
							})
					}).then(res => {
						if(res.data.status == 0) {
							_self.personAcademic.push(...res.data.data.list)
							_self.academicIsLastPage = res.data.data.isLastPage
							_self.academicStatus = 'more'
							if(_self.personAcademic.length == 0) {
								_self.academicStatus = 'noMore'
							}
						}
						uni.hideLoading()
						that.getheight(200)
					})
				}
			})
		},
		components:{
			UniLoadMore,
			PostingsSummarys,
			AcademicSummary
		},
		data() {
			return {
				userId: 0,
				screenWidth: 0, // 屏幕宽度
				screenHeight: 0, // 屏幕高度
				activeSwiper: 0, // 判断是哪一栏被显示（我关注的/所有圈子）
				sortingMode: 0, //  排序方式
				personAcademic: [], // 文章列表
				personDynamic: [], // 动态列表
				// loading前：more; loading中：loading; 没有更多了：noMore
				// 动态获取帖子文章所需参数
				contentText: {
					contentdown: "上拉翻找更多东西~", // loading前的文字显示
					contentrefresh: "正在翻找~~", // loading中的显示
					contentnomore: "没有东西啦！" // 没有更多的显示
				},
				academicStatus: "more",
				dynamicStatus: "more",
				academicNowPage: 1,
				academicIsLastPage: false,
				dynamicNowpage: 1,
				dynamicIsLastpage: false,
				pageSize: 15,
				swiperItemHeight:0
			}
		},
		methods: {
			// 点击上方tab栏的项目时，更新当前index (activeSwiper)
			swichTab: function (e) {
				this.activeSwiper = e.currentTarget.dataset.index
			},
			// 当滑动swiper或者点击导航栏上的选择项目时，重新获取swiper高度
			swiperChange: function (data) {
				this.getheight(200)
				this.activeSwiper = data.detail.current;
			},
			//更新当前swiper高度
			getheight: function(time) {
				setTimeout(() => {
					// 获取当前swiper-item的高度
					let query = uni.createSelectorQuery();
					let page_active = query.select(".page_active");
					page_active.fields({
						size: true
					}, data => {
						// 获取当前设备的高度，取swiper高度为设备高度和swiper-item高度的最大值 （这个方法有bug）
						uni.getSystemInfo({
							success: (data2) => {
								if (data.height <= data2.windowHeight) {
									this.status = 'noMore'
								}
								this.swiperItemHeight = Math.max(data2.windowHeight - 80, data.height) + 20;
							}
						})
					}).exec();
				},time)
			},
			// 更多动态
			moreDynamic() {
				let _self = this
				_self.dynamicNowpage += 1
				_self.dynamicStatus = 'loading'
				_ajax({
					url: '/user/'+_self.userId+'/posts'+'?pageNum='+_self.dynamicNowpage+'&pageSize='+_self.pageSize,
					method: 'get'
				}).then( res => {
					if(res.data.status == 0) {
						_self.personDynamic.push(...res.data.data.list)
						_self.dynamicIsLastpage = res.data.data.isLastPage
						_self.dynamicStatus = 'more'
						this.getheight(200)
					}
				})
			},
			// 更多文章
			moreAcademic() {
				let _self = this
				_self.academicStatus = 'loading'
				_self.academicNowPage += 1
				_ajax({
					url: '/user/'+_self.userId+'/articles'+'?pageNum='+_self.academicNowPage+'&pageSize='+_self.pageSize,
					method: 'get'
				}).then( res => {
					if(res.data.status == 0) {
						_self.personAcademic.push(...res.data.data.list)
						_self.academicIsLastPage = res.data.data.isLastPage
						_self.academicStatus = 'more'
						this.getheight(200)
					}
				})
			},
			// 上拉加载
			loadMore() {
				if(this.activeSwiper == 0) {
					if(this.dynamicIsLastpage) {
						this.dynamicStatus = 'noMore'
					} else {
						this.moreDynamic()
					}
				} else {
					if(this.academicIsLastPage) {
						this.academicStatus = 'noMore'
					} else {
						this.moreAcademic()
					}
				}
			},
		},
		// 上拉加载
		onReachBottom: function() {
			this.loadMore()
			this.getheight()
		},
	}
	
</script>

<style lang="less">
// --status-bar-height为设备状态栏高度
	@statusHeight: var(--status-bar-height);
	.inxupttubiao36::before {
		font-weight: 800;
		color: #FFFFFF;
		font-size: 36rpx;
	}
	.live {
		background-color: @g-background;
		flex-direction: column;
		height: 100%;
		// 占位盒子
		.status_bar {
			height: @statusHeight;
			width: 100%;
			background-color: #FFFFFF;
			position: fixed;
			top: 0rpx;
			z-index: 2;
		}
		.nav {
			display: flex;
			align-items: center;
			justify-content: space-around;
			position: fixed;
			top: 0;
			margin-bottom: 10rpx;
			height: 60rpx;
			width: 100%;
			background-color: #FFFFFF;
			font-size: 32rpx;
			color: @g-gray;
			box-shadow: 0rpx 0rpx 10rpx rgba(0,0,0,.1);
			z-index: 2;
			.listTitle,
			.listTitle_active {
				display: inline-block;
				line-height: 60rpx;
				width: 50%;
				text-align: center;
			}
			.listTitle_active {
				color: #000;
				font-weight: 800;
				border-bottom: 8rpx solid @g-pink;
			}
			.creat {
				margin-left: 50rpx;
				margin-top: 20rpx;
				width: 50rpx;
				height: 50rpx;
				line-height: 50rpx;
				text-align: center;
				border-radius: 10rpx;
				background-color: @g-pink;
			}
		}
		.top_box {
			height: 65rpx;
			width: 100%;
		}
		.page {
			width: 100%;
		}
		uni-load-more {
			width: 100%;
		}
	}
</style>